<style lang="less">
    @import '../../styles/common.less';
</style>

<template>
<div>
        <i-Form :model="model" :label-width="100">
      
                <Card>  
        <p slot="title">
            <Icon type="navicon-round"></Icon>
            用户<span>{{pagename}}</span>
        </p>

                      
            <Form-item label="角色">                
                <i-Select v-model="model.utype" @on-change="changejuese">
                    <Option v-for="item in admintype" :value="item.ID" :key="item.ID">{{item.typename}}</Option>
                </i-Select>
            </Form-item>
            <Form-item label="用户名">
                <i-Input type="text" v-model="model.username" placeholder="用户名">
                </i-Input>
            </Form-item>
             <FormItem label="密码">
                <i-Input :type="this.registration_data.pwdType" ref="pass" v-model="model.password">
                    <i-Button slot="append" icon="eye" @click="changeType"></i-Button>
                </i-Input>
            </FormItem>
            <FormItem label="真实姓名">
                <i-Input v-model="model.penname"/>
            </FormItem>
            <FormItem label="到期时间">
                 <Date-picker type="datetime" v-model="model.endtime" format="yyyy-MM-dd HH:mm:ss" placeholder="到期时间" style="width: 200px"></Date-picker>
            </FormItem>
            <Form-item>
                <i-button type="primary" @click="save">提交</i-button>
            </Form-item>
    </Card>
        </i-Form>
    </div>
    
</template>

<script>
import api from '../../libs/api';
export default {
    name: 'user_indexadd',
    data () {
        return {            
            pagename:"添加",
            model:{},
            admintype:[],
    registration_data:{
      pwdType:"text"
    }
        };
    },
    methods: {        
        changeType(){
        this.registration_data.pwdType = this.registration_data.pwdType==='password'?'text':'password';
        },
        changejuese(option){
        },
        save(){            
            var vm=this;     
            if(vm.model.utype==undefined||vm.model.utype<1)       {
                this.$Message.error("请选择角色");
                return;
            }
            api.post("/user/save",this.model).then(function(res){
                if(vm.model.ID>0){
                    vm.$Message.success("修改成功！");
                    }
                else                    
                    vm.$Message.success("添加成功！");
                    vm.$router.push({
                        name: 'user_index',
                        query: {}
                    });
            })
        },
        getadmintype(callback){
            var vm=this;
            api.get("/admintype/getall").then(function(res){
                vm.admintype=res.data.msg;
                callback();
            })
        },
        init () {
            var vm=this;
            this.getadmintype(function(){
            var id=vm.$route.query.ID;
            if(id>0){
                vm.pagename="编辑"; 
                api.get("/user/get/"+id).then(function(res){
                    vm.model=res.data.msg;   
                    vm.model.password='';
                    setTimeout(function(){vm.registration_data.pwdType="password"},1000);
                    //vm.registration_data.pwdType="password";

                })
            }
            else{
                vm.pagename="添加";             
                vm.$nextTick(() => {
                    var now = new Date();now.setFullYear(now.getFullYear()+1);
                    vm.model={endtime:now};
                })
            }
            })
        }
    },
    mounted () {
        this.init();
    },
    watch: {
        '$route' () {
            this.init();
        }
    }

};
</script>
